import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class consultar
 */
@WebServlet("/view")
public class view extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public view() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");

		PrintWriter out = response.getWriter();

		StringBuilder builder = new StringBuilder();

		builder.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
				+ "Transitional//EN\">\n");
		builder.append("<html>\n");
		builder.append("<head>\n");
		builder.append("<style type=\"text/css\">" + "\n.center" + "\n{\n"
				+ "margin-left: auto;\n" + "margin-right: auto;\n"
				+ "padding: 5px 10px 10px 15px;\n"
				+ "background-color:#FFFFFF;\n" + "opacity:0.8;\n"
				+ "-moz-border-radius: 15px;\n" + "border-radius: 15px;\n"
				+ "width:50%;\n" + "}\n" + "\nul" + "\n{\n"
				+ "padding: 10px 0px 0px 50px;\n" + "}\n" + "</style>\n");

		builder.append("</head>\n");
		builder.append("<body bgcolor=\"#c7cde0\">\n");
		builder.append("<div class=\"center\">\n");
		builder.append("<h1>");
		builder.append(Dades.getQuery(0).getTitle());
		builder.append("</h1>\n");
		builder.append("<table border=\"0\" style=\"width: 100%; \">\n");

		// margin-left: auto; margin-right: auto;

		int numVotesMax = 0;
		long idMax = 0;
		for (Reply maj : Dades.getQuery(0).getReplies()) {
			if (maj.getNumberOfVotes() > numVotesMax) {
				numVotesMax = maj.getNumberOfVotes();
				idMax = maj.getId();
			}
		}

		String bold = "";
		String unbold = "";
		String vots = "vot";

		for (Reply rep : Dades.getQuery(0).getReplies()) {
			rep.setSelected(false);
			builder.append("<tr>");
			builder.append("<td>");
			if (rep.getId() == idMax) {
				bold = "<b>";
				unbold = "</b>";
			} else {
				bold = "";
				unbold = "";
			}
			if (rep.getNumberOfVotes() != 1) {
				vots = "vots";
			} else {
				vots = "vot";
			}

			builder.append(bold + rep.getText() + unbold + "</td><td style=\"text-align:right;\">" + bold
					+ rep.getNumberOfVotes() + " " + vots + unbold + "</td>");

			builder.append("</tr>\n");
		}
		builder.append("</table>\n");
		builder.append("</div>\n");
		builder.append("</body>\n");
		builder.append("</html>");

		out.print(builder.toString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
